import type { PageReq, PageRes } from '../common/commonApi'
import { useApi } from '@/composables/useApi'

export interface CodeMateProjectQuery extends PageReq {
  projectName?: string
}

export interface CodeMate {
  id: number
  projectName: string
  moduleName: string
  moduleCode: number
  priority: number
  remark: string
  parentId: number
  parentType: CodeMateType
}

export interface CodeMateProject extends CodeMate {
  basePackage: string
  moduleList: CodeMateModule[]
}
export interface CodeMateModule extends CodeMate {
  modelList: CodeMateModel[]
}

export const CodeMateModelTypes = [
  { label: '实体', value: 'ENTITY' },
  { label: '字典', value: 'DICT' },
] as const
export type CodeMateModelType = typeof CodeMateModelTypes[number]['value']

export interface CodeMateModel extends CodeMate {
  name: string
  type: CodeMateModelType
  primaryFieldId: number
  fieldList: CodeMateField[]
  methodList: CodeMateMethod[]
  module: CodeMateModule
}

export const CodeMateFieldTypes = [
  { label: '整数', value: 'INTEGER' },
  { label: '字符串', value: 'STRING' },
  { label: '长整数', value: 'LONG' },
  { label: '浮点数', value: 'DOUBLE' },
  { label: '布尔', value: 'BOOLEAN' },
  { label: '日期时间', value: 'DATETIME' },
  { label: '日期', value: 'DATE' },
  { label: '时间', value: 'TIME' },
  { label: '对象', value: 'OBJECT' },
] as const
export type CodeMateFieldType = typeof CodeMateFieldTypes[number]['value']

export interface CodeMateField extends CodeMate {
  name: string
  type: CodeMateFieldType
  modelId: number
  isList: boolean
  required: boolean
  defaultValue: string
  length: number
  pattern: string
  queryType: string
}

export const CodeMateMethodTypes = [
  { label: '创建命令', value: 'CREATE_CMD', name: 'create', remark: '创建' },
  { label: '编辑命令', value: 'MODIFY_CMD', name: 'modify', remark: '编辑' },
  { label: '删除命令', value: 'REMOVE_CMD', name: 'remove', remark: '删除' },
  { label: '详情查询', value: 'DETAIL_QUERY', name: 'detail', remark: '详情查询' },
  { label: '分页查询', value: 'PAGE_QUERY', name: 'page', remark: '分页查询' },
] as const
export type CodeMateMethodType = typeof CodeMateMethodTypes[number]['value']

export interface CodeMateMethod extends CodeMate {
  name: string
  type: CodeMateMethodType
  paramFields: CodeMateField[]
  returnModelId: number
}

export type CodeMateType = 'project' | 'module' | 'model' | 'method' | 'field'
export interface CodeMateTreeNode {
  id: number
  label: string
  remark: string
  type: CodeMateType
  children?: CodeMateTreeNode[]
  data: CodeMate
}

const basePath = '/code/mate'
export const codeMateProjectApi = {
  queryPage: useApi<CodeMateProjectQuery, PageRes<CodeMateProject>>(`${basePath}/project/page`).get,
  findDetail: async (projectName: string) => useApi<any, CodeMateProject>(`${basePath}/project/${projectName}`).get(),
  create: useApi<CodeMateProject, CodeMateProject>(`${basePath}/project`).post,
  modify: useApi<CodeMateProject, CodeMateProject>(`${basePath}/project`).patch,
  remove: async (id: number) => useApi<any, CodeMateProject>(`${basePath}/${id}`).remove(),
}

export const codeMateModuleApi = {
  create: useApi<CodeMateModule, CodeMateModule>(`${basePath}/module`).post,
  modify: useApi<CodeMateModule, CodeMateModule>(`${basePath}/module`).patch,
  remove: async (id: number) => useApi<any, CodeMateModule>(`${basePath}/${id}`).remove(),
}

export const codeMateModelApi = {
  create: useApi<CodeMateModel, CodeMateModel>(`${basePath}/model`).post,
  modify: useApi<CodeMateModel, CodeMateModel>(`${basePath}/model`).patch,
  remove: async (id: number) => useApi<any, CodeMateModel>(`${basePath}/${id}`).remove(),
}

export const codeMateMethodApi = {
  create: useApi<CodeMateMethod, CodeMateMethod>(`${basePath}/method`).post,
  modify: useApi<CodeMateMethod, CodeMateMethod>(`${basePath}/method`).patch,
  remove: async (id: number) => useApi<any, CodeMateMethod>(`${basePath}/${id}`).remove(),
}

export const codeMateFieldApi = {
  create: useApi<CodeMateField, CodeMateField>(`${basePath}/field`).post,
  modify: useApi<CodeMateField, CodeMateField>(`${basePath}/field`).patch,
  remove: async (id: number) => useApi<any, CodeMateField>(`${basePath}/${id}`).remove(),
}
